package com.carcontrol.myplugin4;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;

import com.carcontrol.myplugin4.core.widget.CustomDialog;
import com.carcontrol.myplugin4.utils.LogUtil;

@SuppressLint("NewApi")
public class SplashActivity
		extends AppCompatActivity
		{

	public static boolean isForeground = false;

	private ImageView loadingImage;
	private String imageUrl;
	private String ad_link;
	private String app_type;
	private String app_id;

	LocationManager locationManager;
	String provider = LocationManager.GPS_PROVIDER;
	private static final int PRIVATE_CODE = 1315;
	static private final int REQUEST_CODE_ASK_PERMISSIONS = 101;


	@Override
	public void onCreate(Bundle bundle) {
		super.onCreate(bundle);
		setContentView(R.layout.main_enter);

//		SharedPreferencesUrls.getInstance().putString("access_token", "1");
//		ToastUtils.init(this);

		PluginManager.getInstance().loadPath(this);

//		loadingImage = (ImageView) findViewById(R.id.plash_loading_main);
////		initHttp();
//
//		loadingImage.setOnClickListener(new View.OnClickListener() {
//			@Override
//			public void onClick(View view) {
//				Intent intent = new Intent(SplashActivity.this, ProxyActivity.class);
////                intent.putExtra("className", "cn.qimate.bike.activity.PersonInfoActivity");
////                intent.putExtra("className", "com.hongx.plugin.PersonInfoActivity");
////                intent.putExtra("className", "com.hongx.plugin.MainActivity2");
////                intent.putExtra("className", "com.fengmang.scouringpad.PersonInfoActivity");
////                intent.putExtra("className", "com.fengmang.scouringpad.MainActivity3");
////				intent.putExtra("className", "com.fengmang.myplugin2.TaoMainActivity");
////				intent.putExtra("className", "com.hongx.taopiaopiao.TaoMainActivity");
//				intent.putExtra("className", "com.carcontrol.myplugin3.TaoMainActivity");
//				startActivity(intent);
//			}
//		});

//		init();
//		UIHelper.goToAct(this, MainActivity.class);
	}

	public void click(View view) {
//        UIHelper.goToAct(this, MainActivity2.class);


		Intent intent = new Intent(this, ProxyActivity.class);
//                intent.putExtra("className", "cn.qimate.bike.activity.PersonInfoActivity");
//                intent.putExtra("className", "com.hongx.plugin.PersonInfoActivity");
//                intent.putExtra("className", "com.hongx.plugin.MainActivity2");
//                intent.putExtra("className", "com.fengmang.scouringpad.PersonInfoActivity");
//                intent.putExtra("className", "com.fengmang.scouringpad.MainActivity3");
//				  intent.putExtra("className", "com.hongx.taopiaopiao.TaoMainActivity");
//		intent.putExtra("className", "com.fengmang.scouringpad.TaoMainActivity");
		intent.putExtra("className", "com.carcontrol.myplugin3.TaoMainActivity");
		startActivity(intent);
	}



	private void init() {
		LogUtil.e("init===1", "===");

//		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
//			int checkPermission = this.checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION);
//			if (checkPermission != PackageManager.PERMISSION_GRANTED) {
//				if (shouldShowRequestPermissionRationale(Manifest.permission.ACCESS_FINE_LOCATION)) {
//					requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION},101);
//				} else {
//					requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION},101);
//				}
//				return;
//			}
//		}
//
//		LogUtil.e("init===2", "===");


		/**
		 *
		 * 读写手机状态和身份
		 *
		 */
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
			int checkPermission = this.checkSelfPermission(Manifest.permission.READ_PHONE_STATE);
			if (checkPermission != PackageManager.PERMISSION_GRANTED) {
				if (shouldShowRequestPermissionRationale(Manifest.permission.READ_PHONE_STATE)) {
					requestPermissions(new String[]{Manifest.permission.READ_PHONE_STATE}, 100);
				} else {
					requestPermissions(new String[]{Manifest.permission.READ_PHONE_STATE}, 100);
				}
				return;
			}
		}
		// <!-- 写入扩展存储，向扩展卡写入数据，用于写入离线定位数据 -->
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
			int checkPermission = this.checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE);
			if (checkPermission != PackageManager.PERMISSION_GRANTED) {
				if (shouldShowRequestPermissionRationale(Manifest.permission.READ_EXTERNAL_STORAGE)) {
					requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0);
				} else {
					requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0);
				}
				return;
			}
		}

		if (Build.VERSION.SDK_INT >= 23) {
			int checkPermission = this.checkSelfPermission(Manifest.permission.CAMERA);
			if (checkPermission != PackageManager.PERMISSION_GRANTED) {
				if (shouldShowRequestPermissionRationale(Manifest.permission.CAMERA)) {
					requestPermissions(new String[]{Manifest.permission.CAMERA}, 102);
				} else {
					requestPermissions(new String[]{Manifest.permission.CAMERA}, 102);
				}
				return;
			}
		}

//		if (Build.VERSION.SDK_INT >= 23) {
//			int checkPermission = checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION);
//			if (checkPermission != PackageManager.PERMISSION_GRANTED) {
//				if (shouldShowRequestPermissionRationale(Manifest.permission.ACCESS_FINE_LOCATION)) {
//					requestPermissions(new String[] { Manifest.permission.ACCESS_FINE_LOCATION }, REQUEST_CODE_ASK_PERMISSIONS);
//				} else {
//					requestPermissions(new String[] { Manifest.permission.ACCESS_FINE_LOCATION }, REQUEST_CODE_ASK_PERMISSIONS);
//				}
//				return;
//			}
//		}

//		String access_token = SharedPreferencesUrls.getInstance().getString("access_token", "");
//
//		AppStatusManager.getInstance().setAppStatus(AppStatus.STATUS_NORMAL);
//		if("".equals(access_token)){
//			UIHelper.goToAct(context, LoginActivity.class);
//		}else{
//			UIHelper.goToAct(context, MainActivity.class);
//		}
//
//		finishMine();

	}




	public Thread mThread = new Thread() {
		public void run() {
//			try {
//				mThread.sleep(2 * 1000);
//			} catch (InterruptedException e) {
//				e.printStackTrace();
//			}
//			UIHelper.goToAct(context, MainActivity2.class);
//			finishMine();
		}

		;
	};

//	@Override
//	public void onResume() {
//		isForeground = true;
//		super.onResume();
//	}
//
//			@Override
//			public void onStart() {
//				super.onStart();
//			}
//
//	@Override
//	public void onPause() {
//		isForeground = false;
//		super.onPause();
//	}
//
//	@Override
//	public boolean onKeyDown(int keyCode, KeyEvent event) {
//		if (keyCode == KeyEvent.KEYCODE_BACK) {
//			AppManager.getAppManager().AppExit(this);
//			return true;
//		}
//		return super.onKeyDown(keyCode, event);
//	}


	@Override
	public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
		switch (requestCode) {
			case 0:
				if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
					init();
				} else {
					CustomDialog.Builder customBuilder = new CustomDialog.Builder(this);
					customBuilder.setTitle("温馨提示").setMessage("您需要在设置里打开存储空间权限！")
							.setNegativeButton("取消", new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog, int which) {
									dialog.cancel();
									finish();
								}
							}).setPositiveButton("去设置", new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							dialog.cancel();
							Intent localIntent = new Intent();
							localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
							localIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
							localIntent.setData(Uri.fromParts("package", getPackageName(), null));
							startActivity(localIntent);
							finish();
						}
					});
					customBuilder.create().show();
				}
				return;
			case 100:
				if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
					init();
				} else {
//					showDialog();

					CustomDialog.Builder customBuilder = new CustomDialog.Builder(this);
					customBuilder.setTitle("温馨提示").setMessage("您需要在设置里允许设备信息权限！")
							.setNegativeButton("取消", new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog, int which) {
									dialog.cancel();
									finish();
								}
							}).setPositiveButton("去设置", new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							dialog.cancel();
							Intent localIntent = new Intent();
							localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
							localIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
							localIntent.setData(Uri.fromParts("package", getPackageName(), null));
							startActivity(localIntent);
							finish();
						}
					});
					customBuilder.create().show();

				}
				return;
			case 101:
				if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
					init();
				}else {
					CustomDialog.Builder customBuilder = new CustomDialog.Builder(this);
					customBuilder.setTitle("温馨提示").setMessage("您需要在设置里定位权限！")
							.setNegativeButton("取消", new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog, int which) {
									dialog.cancel();
									finish();
								}
							}).setPositiveButton("去设置", new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							dialog.cancel();
							Intent localIntent = new Intent();
							localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
							localIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
							localIntent.setData(Uri.fromParts("package", getPackageName(), null));
							startActivity(localIntent);
							finish();
						}
					});
					customBuilder.create().show();
				}
				return;

			case 102:
				if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
					init();
				}else {
					CustomDialog.Builder customBuilder = new CustomDialog.Builder(this);
					customBuilder.setTitle("温馨提示").setMessage("您需要在设置里打开相机权限！")
							.setNegativeButton("取消", new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog, int which) {
									dialog.cancel();
									finish();
								}
							}).setPositiveButton("确认", new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							dialog.cancel();
//							requestPermissions(new String[] { Manifest.permission.CAMERA },100);

							Intent localIntent = new Intent();
							localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
							localIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
							localIntent.setData(Uri.fromParts("package", getPackageName(), null));
							startActivity(localIntent);
							finish();
						}
					});
					customBuilder.create().show();

				}
				return;

			default:
				super.onRequestPermissionsResult(requestCode, permissions, grantResults);
		}
	}

}
